<?php

class DWebUser extends CWebUser {
    
    protected function changeIdentity($id, $name, $states) {
        parent::changeIdentity($id, $name, $states);

        //update userId field
        $sessionId = Yii::app()->session->sessionId;
        $sessionTable = Yii::app()->session->sessionTableName;
        $sql = "UPDATE {$sessionTable} SET `userId` = '$id' WHERE `id` = '{$sessionId}'";
        Yii::app()->db->createCommand($sql)->execute();
    }

    public function logout($destroySession=true) {
        parent::logout($destroySession);
        //delete userId field if needed
        if (!$destroySession) {
            $sessionId = Yii::app()->session->sessionId;
            $sessionTable = Yii::app()->session->sessionTableName;
            $sql = "UPDATE {$sessionTable} SET `user_id` = NULL WHERE `id` = '{$sessionId}'";
            Yii::app()->db->createCommand($sql)->execute();
        }
    }

}

?>
